package com.zh.common;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;
import java.util.Scanner;

public class CodeGenerator {

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://192.168.223.128:3309/repo_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC",
                        "root", "123456")
                .globalConfig(builder -> {
                    builder.author("zh") // 作者
                            .outputDir(System.getProperty("user.dir") + "/system-demo/src/main/java"); // 动态输出路径到模块的java目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder -> {
                    builder.parent("com.zh") // 父包设为顶层包名
                            .controller("controller") // 子包路径为 com.zh.controller
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .entity("entity")
                            .pathInfo(Collections.singletonMap(
                                    OutputFile.xml,
                                    System.getProperty("user.dir") + "/system-demo/src/main/resources/mapper" // XML路径指向模块的resources目录
                            ));
                })
                .strategyConfig(builder -> {
                    builder.addInclude("record"); // 设置需要生成的表名
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }


}
